Imports System.Text
<system.Drawing.ToolboxBitmap("TCPClient.bmp")> _
Public Class NetworkClient
    Private Client As System.Net.Sockets.TcpClient
    Private Server As String = "127.0.0.1"
    Private Prt As Int32 = 5000
    Private M_Connected As Boolean = False
    Public Event StatusChanged(ByVal Connected As Boolean)
    Private ret As String
    Public Event DataReceived(ByVal Bytes As Byte())

    Public Property RemoteIP() As String
        Get
            RemoteIP = Server
        End Get
        Set(ByVal value As String)
            Server = value
        End Set
    End Property

    Public Property RemotePort() As Integer
        Get
            RemotePort = Prt
        End Get
        Set(ByVal value As Integer)
            Prt = value
        End Set
    End Property

    Public ReadOnly Property Connected() As Boolean
        Get
            Return M_Connected
        End Get
    End Property
    Public Function Send(ByVal Bytes As Byte()) As Boolean
        Dim STW As System.Net.Sockets.NetworkStream
        STW = Client.GetStream
        Dim BinWrite As New IO.BinaryWriter(STW)
        Dim Len As Int32 = Bytes.Length
        BinWrite.Write(Len)
        BinWrite.Write(Bytes)
        Return True
    End Function
    Public Sub Disconnect()
        BG1.CancelAsync()
        M_Connected = False
        RaiseEvent StatusChanged(False)
    End Sub
    Public Sub Connect()
        ConnectCode(Server, Prt)
    End Sub
    Public Sub Connect(ByVal Remote As String, ByVal Port As Integer)
        ConnectCode(Remote, Port)
    End Sub
    Private Sub ConnectCode(ByVal S As String, ByVal P As Integer)
        Try
            Client = New System.Net.Sockets.TcpClient(S, P)
            M_Connected = True
            Dim lingerOption As New Net.Sockets.LingerOption(True, 10)
            Client.LingerState = lingerOption
            BG1.RunWorkerAsync(Client)
            RaiseEvent StatusChanged(True)
        Catch
            M_Connected = False
        End Try

    End Sub
    Private Sub BG1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG1.DoWork
        Dim Block As Integer = 8 * 1024

        Dim ST As System.Net.Sockets.NetworkStream
        Dim CL As System.Net.Sockets.TcpClient
        CL = e.Argument
        ST = CL.GetStream
        Do
            If ST.DataAvailable Then
                Dim BinRead As New IO.BinaryReader(ST)
                Dim L As Int32 = BinRead.ReadInt32
                Dim D As Byte() = BinRead.ReadBytes(L)
                BG1.ReportProgress(100, D)
            End If
            If BG1.CancellationPending = True Then
                M_Connected = False
                Exit Do
            End If
        Loop
    End Sub

    Private Sub BG1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BG1.ProgressChanged
        Dim BytesIn As Byte()
        If e.ProgressPercentage = 100 Then
            BytesIn = e.UserState
            RaiseEvent DataReceived(BytesIn)
        End If
    End Sub

    Private Sub BG1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BG1.RunWorkerCompleted
        Do
            client.Close()
        Loop Until Client.Connected = False
        M_Connected = False
    End Sub
End Class

